{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "67f4d810",
   "metadata": {},
   "source": [
    "### 降维-手写数字识别"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "3b9da4fb",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-06-17T12:50:53.058866Z",
     "start_time": "2022-06-17T12:50:52.377202Z"
    }
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "34ac8ff7",
   "metadata": {},
   "source": [
    "#### 加载数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "4773514e",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-06-17T12:50:57.710875Z",
     "start_time": "2022-06-17T12:50:54.701509Z"
    },
    "collapsed": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>label</th>\n",
       "      <th>pixel0</th>\n",
       "      <th>pixel1</th>\n",
       "      <th>pixel2</th>\n",
       "      <th>pixel3</th>\n",
       "      <th>pixel4</th>\n",
       "      <th>pixel5</th>\n",
       "      <th>pixel6</th>\n",
       "      <th>pixel7</th>\n",
       "      <th>pixel8</th>\n",
       "      <th>...</th>\n",
       "      <th>pixel774</th>\n",
       "      <th>pixel775</th>\n",
       "      <th>pixel776</th>\n",
       "      <th>pixel777</th>\n",
       "      <th>pixel778</th>\n",
       "      <th>pixel779</th>\n",
       "      <th>pixel780</th>\n",
       "      <th>pixel781</th>\n",
       "      <th>pixel782</th>\n",
       "      <th>pixel783</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 785 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "   label  pixel0  pixel1  pixel2  pixel3  pixel4  pixel5  pixel6  pixel7  \\\n",
       "0      1       0       0       0       0       0       0       0       0   \n",
       "1      0       0       0       0       0       0       0       0       0   \n",
       "2      1       0       0       0       0       0       0       0       0   \n",
       "3      4       0       0       0       0       0       0       0       0   \n",
       "4      0       0       0       0       0       0       0       0       0   \n",
       "\n",
       "   pixel8  ...  pixel774  pixel775  pixel776  pixel777  pixel778  pixel779  \\\n",
       "0       0  ...         0         0         0         0         0         0   \n",
       "1       0  ...         0         0         0         0         0         0   \n",
       "2       0  ...         0         0         0         0         0         0   \n",
       "3       0  ...         0         0         0         0         0         0   \n",
       "4       0  ...         0         0         0         0         0         0   \n",
       "\n",
       "   pixel780  pixel781  pixel782  pixel783  \n",
       "0         0         0         0         0  \n",
       "1         0         0         0         0  \n",
       "2         0         0         0         0  \n",
       "3         0         0         0         0  \n",
       "4         0         0         0         0  \n",
       "\n",
       "[5 rows x 785 columns]"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = pd.read_csv('./digits.csv')\n",
    "data.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "c2ad90c4",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-06-17T12:51:01.550354Z",
     "start_time": "2022-06-17T12:51:01.507706Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    1\n",
       "1    0\n",
       "2    1\n",
       "3    4\n",
       "4    0\n",
       "Name: label, dtype: int64"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>pixel0</th>\n",
       "      <th>pixel1</th>\n",
       "      <th>pixel2</th>\n",
       "      <th>pixel3</th>\n",
       "      <th>pixel4</th>\n",
       "      <th>pixel5</th>\n",
       "      <th>pixel6</th>\n",
       "      <th>pixel7</th>\n",
       "      <th>pixel8</th>\n",
       "      <th>pixel9</th>\n",
       "      <th>...</th>\n",
       "      <th>pixel774</th>\n",
       "      <th>pixel775</th>\n",
       "      <th>pixel776</th>\n",
       "      <th>pixel777</th>\n",
       "      <th>pixel778</th>\n",
       "      <th>pixel779</th>\n",
       "      <th>pixel780</th>\n",
       "      <th>pixel781</th>\n",
       "      <th>pixel782</th>\n",
       "      <th>pixel783</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 784 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "   pixel0  pixel1  pixel2  pixel3  pixel4  pixel5  pixel6  pixel7  pixel8  \\\n",
       "0       0       0       0       0       0       0       0       0       0   \n",
       "1       0       0       0       0       0       0       0       0       0   \n",
       "2       0       0       0       0       0       0       0       0       0   \n",
       "3       0       0       0       0       0       0       0       0       0   \n",
       "4       0       0       0       0       0       0       0       0       0   \n",
       "\n",
       "   pixel9  ...  pixel774  pixel775  pixel776  pixel777  pixel778  pixel779  \\\n",
       "0       0  ...         0         0         0         0         0         0   \n",
       "1       0  ...         0         0         0         0         0         0   \n",
       "2       0  ...         0         0         0         0         0         0   \n",
       "3       0  ...         0         0         0         0         0         0   \n",
       "4       0  ...         0         0         0         0         0         0   \n",
       "\n",
       "   pixel780  pixel781  pixel782  pixel783  \n",
       "0         0         0         0         0  \n",
       "1         0         0         0         0  \n",
       "2         0         0         0         0  \n",
       "3         0         0         0         0  \n",
       "4         0         0         0         0  \n",
       "\n",
       "[5 rows x 784 columns]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "y = data['label']\n",
    "X = data.iloc[:,1:]\n",
    "display(y.head(),X.head())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "11944687",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-06-17T12:30:03.500364Z",
     "start_time": "2022-06-17T12:30:03.488399Z"
    }
   },
   "outputs": [],
   "source": [
    "X.shape"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "59a785ce",
   "metadata": {},
   "source": [
    "#### 展示数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "feaeaf2d",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-06-17T12:30:04.296261Z",
     "start_time": "2022-06-17T12:30:03.503361Z"
    }
   },
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ea7e2984",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-06-17T12:30:04.312168Z",
     "start_time": "2022-06-17T12:30:04.301176Z"
    }
   },
   "outputs": [],
   "source": [
    "y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "56d34af3",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-06-17T12:30:04.531411Z",
     "start_time": "2022-06-17T12:30:04.317144Z"
    }
   },
   "outputs": [],
   "source": [
    "# 784像素，28行和28列组成的 \n",
    "image = X.iloc[41997].values.reshape(28,28)\n",
    "plt.figure(figsize=(2,2))\n",
    "plt.imshow(image,cmap='gray')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a7a643fa",
   "metadata": {},
   "source": [
    "#### 数据拆分"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "1bdfee4a",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-06-17T12:51:12.001149Z",
     "start_time": "2022-06-17T12:51:10.951515Z"
    },
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(41000, 784)"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "(1000, 784)"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "from sklearn.model_selection import train_test_split\n",
    "X_train,X_test,y_train,y_test = train_test_split(X,y,test_size = 1000,random_state = 0)\n",
    "display(X_train.shape,X_test.shape)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "76b08155",
   "metadata": {},
   "source": [
    "#### 建模【逻辑斯蒂回归】"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0ff22be9",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-06-17T12:32:55.963557Z",
     "start_time": "2022-06-17T12:31:01.195299Z"
    }
   },
   "outputs": [],
   "source": [
    "%%time\n",
    "# from sklearn.linear_model import LogisticRegression\n",
    "from sklearn.neighbors import KNeighborsClassifier\n",
    "\n",
    "model = KNeighborsClassifier()\n",
    "model.fit(X_train,y_train)\n",
    "y_ = model.predict(X_test)\n",
    "display(y_test[:20],y_[:20])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9215319c",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-06-17T12:37:27.496223Z",
     "start_time": "2022-06-17T12:35:46.212962Z"
    }
   },
   "outputs": [],
   "source": [
    "%%time\n",
    "model.score(X_test,y_test) #模型进行预测时花费时间比较长"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "86b281e6",
   "metadata": {},
   "source": [
    "#### 数据降维"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "cdb74637",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-06-17T13:11:28.113088Z",
     "start_time": "2022-06-17T13:11:27.976245Z"
    }
   },
   "outputs": [],
   "source": [
    "from sklearn.decomposition import PCA"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "3a979b80",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-06-17T13:11:41.089263Z",
     "start_time": "2022-06-17T13:11:34.012738Z"
    },
    "collapsed": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(42000, 784)"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "(42000, 154)"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([[-6.61595780e+02, -6.99311328e+02,  1.83282040e+02,\n",
       "         1.20611394e+02, -8.10818105e+01,  4.89461886e+02,\n",
       "        -6.83470838e+02,  8.55593866e+01,  3.48548052e+02,\n",
       "         2.02978545e+02, -3.64559949e+02,  2.12657559e+01,\n",
       "         4.04441449e+02, -9.70525455e+01,  6.18399356e+01,\n",
       "        -8.67800272e+01,  1.76581436e+01,  2.85484696e+02,\n",
       "         1.88273028e+01,  2.07642731e+02,  4.42436003e+01,\n",
       "         2.21894370e+02,  5.72274592e+01, -1.48674962e+02,\n",
       "         1.43435889e+01, -4.15560311e+01, -3.33723659e+02,\n",
       "         2.08978881e+02,  5.98136306e+01,  8.45544647e+01,\n",
       "         6.94293060e+00, -5.29995065e+00,  1.95709336e+02,\n",
       "        -5.32292285e+01, -6.88959872e+00, -1.46886583e+02,\n",
       "        -4.94066227e+01, -8.80639802e+01, -1.61589210e+02,\n",
       "        -1.40787617e+02,  1.04959060e+02,  2.00310607e+01,\n",
       "         4.15619445e+01, -3.65287408e+01, -2.06185633e+02,\n",
       "         3.34091629e+01, -5.41183777e+01, -5.62953193e+01,\n",
       "        -7.53699522e+01,  6.90935930e+01,  7.33123981e+01,\n",
       "        -1.18059337e+02, -9.73279782e+01, -2.95899110e+01,\n",
       "         4.46566576e+01,  2.27470430e+01,  1.12695968e+02,\n",
       "        -7.15693177e+01,  1.47451510e+01, -3.70484637e+00,\n",
       "         2.11584606e+00,  4.29406155e+01, -4.34704246e+01,\n",
       "        -9.63272075e+01,  3.64223881e+01,  1.18598347e+02,\n",
       "        -1.77235255e+01,  5.29303605e+01,  5.25332827e+01,\n",
       "        -2.00622353e+01, -8.88999545e+01,  1.36938121e+01,\n",
       "        -1.50529835e+01, -5.39919251e+01,  1.05550832e+02,\n",
       "        -5.03088487e+01, -7.74394518e+00, -1.73007189e+01,\n",
       "        -3.64719081e+01, -5.50678419e+01, -6.35349144e+01,\n",
       "        -5.62571565e+01,  2.69855005e+01, -3.60697595e+01,\n",
       "        -9.05306937e+00, -5.79513256e+00,  3.96563814e+01,\n",
       "         3.88313575e+01,  1.44954749e+00, -1.73833777e+01,\n",
       "        -5.40012250e+01,  8.01308775e+01, -1.08551289e+01,\n",
       "         6.08951080e+01,  1.36194560e+01, -3.01334206e+00,\n",
       "         3.02487724e+01,  3.06759331e+01, -6.19120027e+01,\n",
       "        -3.19584159e+00,  4.00223122e+01, -3.34308164e+00,\n",
       "         1.84193391e+01,  7.26215209e+00,  1.13363672e+01,\n",
       "         3.23230244e+01,  6.85752152e-02,  2.61286835e+01,\n",
       "         1.40324266e+01, -1.12007991e+01,  1.13033938e+01,\n",
       "        -4.99136166e+01,  4.85901221e+01, -3.72712473e+01,\n",
       "         1.07513167e+01,  1.79894516e+00, -1.51096415e+01,\n",
       "         1.47954742e+00,  5.96809033e+00,  6.56583371e+01,\n",
       "        -6.61924098e+00, -2.49765684e+01, -1.10602525e+01,\n",
       "        -1.65329098e+01,  1.28775170e+01,  2.35936793e+01,\n",
       "        -2.13854589e+01, -3.38218724e+01,  4.54300062e+01,\n",
       "         2.98272565e+01,  3.20597698e+01,  2.08734236e+01,\n",
       "         6.73706581e-01, -2.47116248e+01,  1.42380020e+01,\n",
       "        -5.14823407e+00,  2.03060774e+01, -4.24404029e+00,\n",
       "         2.74662728e+01,  2.49457426e+01, -6.55663259e+00,\n",
       "        -1.06552736e+01, -5.70361182e-01, -3.10174825e+00,\n",
       "        -1.70984362e+01, -5.83392490e+01,  1.58231499e+01,\n",
       "        -9.28701108e+00, -1.84827260e+01, -1.85209618e+01,\n",
       "        -5.67210146e+00, -3.36801973e+01, -5.44911389e+01,\n",
       "        -9.79971911e+00],\n",
       "       [ 1.70145168e+03, -3.60551556e+02, -5.01805594e+02,\n",
       "         3.35423656e+02, -4.42378933e+02,  7.38404049e+02,\n",
       "         6.53875438e+02, -1.76600677e+02, -7.52017489e+00,\n",
       "         6.78462729e+01,  3.42218036e+01,  4.65518418e+01,\n",
       "        -7.04357747e+01, -3.42692097e+02,  3.77839952e+02,\n",
       "        -5.66582709e+00,  3.17765748e+02,  8.76126182e+01,\n",
       "        -9.45311679e+01, -1.75028270e+02, -2.13086598e+02,\n",
       "        -2.72411966e+02,  7.16761158e+00, -2.26351490e+01,\n",
       "        -3.46085889e+01,  2.64486976e+02, -7.66219279e+01,\n",
       "         1.40261297e+01, -8.04273396e+01, -8.76849867e+01,\n",
       "        -2.78225440e+01,  1.88784153e+02,  1.15953010e+02,\n",
       "         5.79216955e+01, -1.04447859e+01, -9.17363969e+01,\n",
       "        -1.10514502e+02, -4.06033466e+01,  1.11846730e+02,\n",
       "         8.71131357e+01,  1.29083932e+01,  3.61403609e+00,\n",
       "         9.69595459e+00, -4.54692379e+01, -1.09212609e+02,\n",
       "        -4.30923540e+01, -4.06763000e+01, -1.51213971e+02,\n",
       "         1.18322989e+02, -2.81103331e+00,  8.24000681e+01,\n",
       "        -1.30732237e+02,  1.04359128e+02, -2.87063844e+01,\n",
       "         9.08563145e+01, -6.80761315e+01,  1.20763651e+02,\n",
       "         4.15327152e+00,  1.37151596e+01, -1.53550509e+02,\n",
       "         6.48142955e+01, -9.93565544e+01, -5.09957206e+01,\n",
       "        -5.03832932e+01, -8.65479701e+01, -1.00344086e+02,\n",
       "         1.42230986e+01, -1.01693485e+02,  5.14566575e+01,\n",
       "        -7.14545714e+01, -5.49704534e+01,  4.98682959e+01,\n",
       "        -9.67656929e+01,  4.57448853e+00, -7.46269237e+01,\n",
       "         2.61507478e+01, -1.20750221e+02, -1.21755999e+02,\n",
       "        -9.10553442e+01,  2.77135345e+01, -6.40562448e+01,\n",
       "         1.96350686e+01, -2.68977531e+01, -1.93850803e+01,\n",
       "        -4.90527783e+01,  3.09887052e+01,  8.83521542e+01,\n",
       "        -5.76627974e+01,  2.50123452e+01, -2.78118447e+01,\n",
       "        -4.91492700e+01, -7.74837879e+01, -7.46169539e+01,\n",
       "         1.36761793e+02, -2.21230577e+01,  4.61429728e+01,\n",
       "         2.87432992e+01, -4.63809154e+01,  2.82468042e-01,\n",
       "        -9.21023015e+01, -9.57079848e+01, -2.68850849e+01,\n",
       "        -2.24328362e+01,  1.28613688e+01,  3.12120394e+01,\n",
       "         3.78667410e+00,  7.24426778e+01, -4.90498959e+01,\n",
       "         7.59582760e+01, -3.34000564e+01,  5.03948367e+01,\n",
       "         1.51187415e+01,  1.00554104e+01,  2.57652673e+01,\n",
       "         4.85278335e+01, -8.89046863e+01,  2.31881486e+01,\n",
       "         4.35448786e+01,  7.75342325e+00, -4.66799744e+01,\n",
       "        -2.92814811e+01,  6.66177307e+01, -2.92195228e+01,\n",
       "        -1.01330724e+01,  3.55674762e+01, -5.53170164e+01,\n",
       "        -5.43178943e+01, -5.47585793e+01,  4.01446867e+01,\n",
       "         2.95289123e+01,  1.13607589e+01,  2.69898293e+01,\n",
       "         1.87278278e+01, -4.86957839e+01, -3.87900164e+01,\n",
       "         1.58172124e+01,  2.66032806e+00, -2.12699093e+01,\n",
       "        -3.39283913e+00,  3.25941090e+01, -4.95273383e+01,\n",
       "        -1.19803715e+01,  2.22178557e+01,  1.52321606e+00,\n",
       "        -4.34070141e+01, -3.64909623e+01,  4.09806457e+01,\n",
       "        -1.06851215e+01,  6.62211767e+01,  3.22940309e+01,\n",
       "         1.08598132e+01, -1.85578044e+01,  1.17929699e+01,\n",
       "         4.09376819e+01],\n",
       "       [-8.86894434e+02, -2.93765782e+02,  6.71553099e+01,\n",
       "         7.82637628e+01, -4.73715921e+02, -3.23540638e+02,\n",
       "         4.37799098e+02, -3.05377740e+02, -1.95295503e+02,\n",
       "        -2.56260228e+01,  3.67595529e+02,  2.52106647e+02,\n",
       "         5.45578883e+01, -5.85908920e+01,  1.63090574e+02,\n",
       "        -1.62494140e+02, -1.00318548e+02, -1.55369350e+02,\n",
       "         7.02048839e+01, -1.82797458e+02, -1.04940439e+02,\n",
       "         1.28626995e+02,  1.52009387e+02,  5.23957225e+01,\n",
       "        -1.12874077e+02,  8.81437286e+01, -2.29586767e+02,\n",
       "         1.06927395e+02, -1.05151323e+01,  1.21363326e+02,\n",
       "         8.81425935e+01,  4.06864624e+01, -4.89212903e+01,\n",
       "        -1.18051804e+02, -1.68743865e+01, -1.79646525e+00,\n",
       "        -5.18193048e+01,  6.64473434e+01, -1.15008861e+02,\n",
       "        -3.82815600e+01,  1.60549673e+02,  3.02159364e+01,\n",
       "         7.95183866e+01,  1.10227196e+02,  3.35974573e+01,\n",
       "         1.34010930e+02,  6.79676195e+00, -1.12585583e+01,\n",
       "        -6.76870236e+01, -4.46430476e+01, -4.06015004e+01,\n",
       "         5.89196602e+01, -1.63459609e+01,  6.73010165e-01,\n",
       "        -2.71289283e+01, -4.32034805e+01, -4.10370095e+01,\n",
       "        -2.22570040e+01,  9.53603455e+01, -6.07336681e+01,\n",
       "         1.04715804e+02, -7.85031319e+00, -7.73380818e+01,\n",
       "        -1.95167098e+00, -1.09032511e+01, -5.42613021e+01,\n",
       "        -9.32714842e+01,  6.97832925e-01, -4.92824731e+01,\n",
       "         7.18092321e+01,  3.88141733e+01,  5.38351333e+01,\n",
       "         6.10290154e+01, -2.32615423e+01, -2.36596752e+01,\n",
       "         8.02447283e+01, -3.92682995e+00, -1.00187382e+01,\n",
       "         1.03873881e+01, -7.03179103e+00,  1.10624254e+02,\n",
       "         2.71506364e+01, -1.92435214e+01,  5.16848951e+01,\n",
       "         1.43871171e+01,  2.75343447e+01, -5.40658819e+01,\n",
       "         4.94337194e+01,  2.56945120e+01,  3.25121921e+01,\n",
       "         5.69744242e+00, -4.76823021e+01, -3.99170629e+00,\n",
       "         6.19715493e+00, -2.40840705e+01, -3.88714369e+00,\n",
       "        -3.10129487e+00,  8.39920146e+01, -8.26400271e+00,\n",
       "        -1.22765770e+02, -3.49254298e+01,  9.76447268e+00,\n",
       "        -4.53428082e+01, -8.97723873e+00,  8.41053145e+00,\n",
       "        -2.67659808e+01, -4.81520970e+00,  7.21262021e+01,\n",
       "        -7.54685090e+00,  1.42584261e+01, -3.99130566e+00,\n",
       "        -2.32812102e+01, -5.32141364e+01,  6.49398772e+00,\n",
       "         1.79971056e+01, -2.60001432e+01,  1.76192622e+01,\n",
       "         2.50932785e+01, -2.74308464e+01, -2.54059641e+01,\n",
       "        -9.12780187e-01,  1.77040308e+01,  1.80411955e+00,\n",
       "         1.69189559e+01,  3.61654453e+00, -2.20463124e+01,\n",
       "        -3.09807698e+01,  4.00492801e+01,  4.37472525e+01,\n",
       "         4.84945813e+01, -4.58043686e+01,  8.23717741e+01,\n",
       "         1.66444036e+01, -3.98177957e+01,  4.74083457e+00,\n",
       "         1.36895921e+01,  2.32459561e+01, -5.15853958e+01,\n",
       "         1.89833906e+01,  1.23794336e+01, -5.21912471e+01,\n",
       "        -1.20142424e+01, -1.56003041e+01, -1.41413307e+01,\n",
       "        -1.38810111e+01,  3.76859877e+01,  2.93525063e+01,\n",
       "        -1.29888909e+01, -6.04163784e+01,  5.40199634e+00,\n",
       "         1.13841986e+01,  1.31771706e+01, -1.36829320e+01,\n",
       "         2.05854394e+01],\n",
       "       [-1.65755602e+02,  3.00182761e+02, -6.41454852e+01,\n",
       "         7.59706261e+02, -4.25844379e+02,  1.57390337e+02,\n",
       "        -3.04099140e+02,  2.76408982e+02, -4.58672154e+01,\n",
       "        -2.95477581e+02, -2.74648031e+00, -2.56884297e+02,\n",
       "        -8.77341898e+01, -1.75361267e+02,  4.00517078e+01,\n",
       "        -8.75363241e+01,  5.42788813e+01,  1.99848998e+02,\n",
       "        -1.18262009e+01,  2.98091710e+02, -2.32160006e+02,\n",
       "        -8.98548411e+01,  2.92732889e+02,  1.25822780e+02,\n",
       "        -6.87010304e+01, -1.93423679e+02, -1.84238504e+02,\n",
       "         8.28971095e+01,  2.14449496e+02,  1.91178375e+02,\n",
       "        -1.02968026e+02, -3.05483875e+01,  1.16253694e+02,\n",
       "         1.52968593e+01,  1.17492107e+02,  1.14581023e+02,\n",
       "        -6.62112174e+01,  7.46613408e+01,  2.76481750e+01,\n",
       "         1.19080519e+02, -6.18255697e+01,  1.43193263e+02,\n",
       "         5.67925544e+01,  1.49172860e+02,  2.12810009e+02,\n",
       "         2.60773853e+02,  2.10584889e+02,  1.02951733e+02,\n",
       "        -5.33945251e+01,  3.02930240e+01,  6.02796964e+01,\n",
       "         3.11222431e+00,  1.49053354e+02, -7.29759301e+01,\n",
       "        -2.44992479e+02,  5.53495777e+00,  9.09065039e+01,\n",
       "         1.24460604e+02,  1.27360102e+02,  1.31523240e+02,\n",
       "        -8.31338390e+00,  1.95892354e+02, -9.62353337e+01,\n",
       "         1.43062309e+01, -2.15505756e+01, -3.52351052e+01,\n",
       "         5.33091391e+01,  9.80904332e+01,  1.24472966e+02,\n",
       "         1.22015947e+02, -4.78436557e+01, -8.87267924e+01,\n",
       "        -5.75032543e+00,  5.35178808e+01,  2.93858501e+01,\n",
       "         4.00440098e+01,  4.19195442e+01,  8.97934120e+00,\n",
       "        -4.82343074e+01, -9.53922584e+01, -1.49150369e+02,\n",
       "        -3.49906640e+01, -1.27953321e+02,  6.63694239e+01,\n",
       "         3.89104044e+01, -1.95003698e+02, -9.29179244e+01,\n",
       "        -5.88494323e+01, -3.06382983e+01, -3.55512326e+01,\n",
       "         1.71582930e+01,  1.67045900e+02, -2.90435170e+01,\n",
       "        -5.75872865e+01, -1.20187457e+02,  1.63978000e+01,\n",
       "         4.75489900e+01, -9.04425601e+01,  8.37901468e+01,\n",
       "         1.26101041e+02, -1.38993405e+00, -5.82456021e+01,\n",
       "         1.39449369e+02,  2.59664860e+01, -1.48040986e+00,\n",
       "        -2.62898717e+01,  6.15136274e+00, -1.78636386e+01,\n",
       "        -1.44372048e+01, -1.02234554e+02, -1.04831731e+02,\n",
       "        -3.79006359e+01, -4.47181651e+01,  8.91963136e+01,\n",
       "         2.44239024e+01, -4.06833843e+01,  8.62335881e+01,\n",
       "         1.14836970e+02,  5.78946696e+01,  2.25685976e+01,\n",
       "        -9.71623801e+01,  4.07154248e+00, -1.63508058e+01,\n",
       "        -1.96085224e+01,  1.99010040e+01,  1.27243997e+02,\n",
       "         1.25261449e+01, -4.55635613e+01,  5.91789577e+01,\n",
       "        -5.67814625e+01, -1.39057629e+02, -1.09812925e+02,\n",
       "         1.00258856e+02,  2.05797772e+01,  5.79984796e+00,\n",
       "         6.28769275e+00,  9.73633780e+01, -1.87307032e+01,\n",
       "        -9.51149216e+00, -1.42313640e+01,  2.11811902e+01,\n",
       "        -9.20329177e+01,  8.89747285e+00,  7.70305374e+01,\n",
       "        -4.63887074e+00,  6.81820621e+01,  9.48621812e+01,\n",
       "        -1.56225652e+01, -9.62648230e+01,  4.76479305e+01,\n",
       "        -4.18029343e+01, -1.06302453e+01, -2.05083797e+01,\n",
       "         1.18396407e+01],\n",
       "       [ 1.92370972e+03, -4.49153069e+02, -5.48613023e+02,\n",
       "         1.88555147e+02, -6.51736273e+02,  9.90063824e+02,\n",
       "         5.64507042e+02, -2.55915217e+02,  1.24914693e+02,\n",
       "         1.77566843e+02, -1.94919879e+01,  3.33721902e+02,\n",
       "        -2.13056379e+02, -3.54643577e+02,  9.34767364e+01,\n",
       "         2.67942260e+01,  2.32994137e+02, -5.47396479e+01,\n",
       "        -4.53425662e+01, -2.56209640e+02, -1.56581730e+01,\n",
       "        -1.31146570e+02, -1.40317010e+02, -5.69322317e+01,\n",
       "         1.75858593e+02, -8.68099092e+00,  5.03125199e+01,\n",
       "        -1.62724694e+02, -6.94699872e+01,  2.47467752e+01,\n",
       "        -3.60449159e+01, -5.93592246e+00,  1.33813261e+01,\n",
       "        -2.90455335e+01, -1.33364115e+02,  6.87869599e+01,\n",
       "        -7.93284700e+01,  3.85968565e+01, -1.42551687e+01,\n",
       "        -8.99845184e+01,  1.22302622e+02, -8.13655143e+01,\n",
       "         5.19590526e+01, -4.50134784e+01, -1.09727060e+02,\n",
       "         5.20005154e+01,  1.94418663e+01, -2.91526976e+01,\n",
       "         6.50525413e+00,  1.12702451e+02, -9.54009600e+01,\n",
       "        -8.10419486e+01,  2.89031730e+01,  1.03378612e+02,\n",
       "         1.06039713e+02, -5.35574318e+01,  8.50018174e+01,\n",
       "         5.15031029e+01,  2.07020915e+01, -1.25911818e+01,\n",
       "         7.59294369e+01, -6.02391737e+01,  7.23047147e+01,\n",
       "         5.13713353e+01,  1.56050276e+02, -9.27189063e+01,\n",
       "         6.59889761e+01, -8.47849142e+01, -8.66717739e+01,\n",
       "         8.60858508e+01,  7.00212851e+01,  8.36576084e+01,\n",
       "        -2.34220513e+02, -8.11291126e+00, -9.00489503e+01,\n",
       "        -3.55214508e+01, -1.10326687e+00, -3.27045310e+01,\n",
       "        -1.07970844e+01,  7.34549119e+01, -3.45837161e+01,\n",
       "        -3.52878254e+01, -6.38473438e+01, -3.10726111e+01,\n",
       "        -6.98869545e+01,  4.96017648e+01,  5.04410657e+01,\n",
       "         7.19274779e+01,  3.85794055e+00,  3.75004188e+01,\n",
       "        -9.73077590e+01, -4.49733986e+01, -8.11436568e+01,\n",
       "         1.43273216e+02, -1.45373746e+02,  4.94979186e+01,\n",
       "         4.16328101e+01, -1.10732080e+02, -6.12800737e+01,\n",
       "        -6.74352617e+01, -2.54112008e+01,  1.05102701e+01,\n",
       "        -7.73559659e-01, -7.49674733e+00, -6.96843645e+01,\n",
       "        -3.06581087e+01,  8.19623544e+01, -2.65623443e+01,\n",
       "        -6.53445314e+01, -5.80269421e+01,  4.44027877e+01,\n",
       "         1.75713112e+01, -1.74472946e+01, -1.05012497e+02,\n",
       "         7.80726431e+01, -3.29483168e+01, -2.11975036e+01,\n",
       "         9.22540234e+00, -3.58052604e+01,  4.61742703e+01,\n",
       "        -4.33084640e+01,  9.35875945e+01,  4.47226628e+01,\n",
       "         2.09315707e+00,  3.67702329e+01, -6.11904296e+01,\n",
       "         2.08885377e+00, -5.07955385e+01, -5.18843609e-01,\n",
       "         1.25974155e+00,  1.70192925e+00,  3.23790650e+01,\n",
       "         3.35080578e+01,  1.53509580e+00,  2.16189806e+01,\n",
       "         4.95752311e+01, -2.25657900e+01, -3.77575889e+01,\n",
       "         5.67435740e+01, -1.50723215e+01, -6.66099126e+00,\n",
       "         4.46640852e+01, -2.11696052e+01, -3.64816836e+01,\n",
       "         5.08387059e+01,  1.07749270e+01,  1.00325811e+01,\n",
       "         2.25602038e+01,  9.79546647e+01, -7.58199956e+01,\n",
       "         9.41112601e-01,  5.04847191e+01, -4.47541051e+01,\n",
       "        -1.04896580e+01]])"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "pca = PCA(n_components=0.95) \n",
    "# 标准化处理，算法KNN，距离计算，不需要标准化，如果是梯度下降算法【必须归一化】\n",
    "X_pca = pca.fit_transform(X)\n",
    "display(X.shape,X_pca.shape)\n",
    "display(X_pca[:5])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "92c0d577",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-06-17T13:11:57.453180Z",
     "start_time": "2022-06-17T13:11:57.311489Z"
    }
   },
   "outputs": [],
   "source": [
    "X_train_pca,X_test_pca,y_train,y_test = train_test_split(X_pca,y,test_size = 1000,random_state = 0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6e7e672c",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-06-17T12:42:45.105982Z",
     "start_time": "2022-06-17T12:42:25.272511Z"
    }
   },
   "outputs": [],
   "source": [
    "%%time\n",
    "knn = KNeighborsClassifier()\n",
    "\n",
    "knn.fit(X_train_pca,y_train)\n",
    "\n",
    "y_ = knn.predict(X_test_pca)\n",
    "display(y_test.values[:20],y_[:20])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6743bfb9",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-06-17T12:43:38.044999Z",
     "start_time": "2022-06-17T12:43:19.900909Z"
    }
   },
   "outputs": [],
   "source": [
    "knn.score(X_test_pca,y_test)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "11f9f2e3",
   "metadata": {},
   "source": [
    "#### 探索逻辑斯蒂回归【出错】"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ae845c6c",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-06-17T12:46:10.057454Z",
     "start_time": "2022-06-17T12:46:08.105292Z"
    }
   },
   "outputs": [],
   "source": [
    "pip list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cd929b08",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-06-17T12:48:54.943990Z",
     "start_time": "2022-06-17T12:48:53.772953Z"
    }
   },
   "outputs": [],
   "source": [
    "pip list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "04d25355",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-06-17T13:00:14.297719Z",
     "start_time": "2022-06-17T12:52:04.217484Z"
    },
    "collapsed": true
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "d:\\soft\\python\\396\\lib\\site-packages\\sklearn\\linear_model\\_logistic.py:444: ConvergenceWarning: lbfgs failed to converge (status=1):\n",
      "STOP: TOTAL NO. of ITERATIONS REACHED LIMIT.\n",
      "\n",
      "Increase the number of iterations (max_iter) or scale the data as shown in:\n",
      "    https://scikit-learn.org/stable/modules/preprocessing.html\n",
      "Please also refer to the documentation for alternative solver options:\n",
      "    https://scikit-learn.org/stable/modules/linear_model.html#logistic-regression\n",
      "  n_iter_i = _check_optimize_result(\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "16275    3\n",
       "19204    6\n",
       "18518    9\n",
       "25780    5\n",
       "16228    6\n",
       "15824    5\n",
       "29252    6\n",
       "28482    0\n",
       "13779    0\n",
       "25912    1\n",
       "27141    7\n",
       "21848    1\n",
       "32576    5\n",
       "7975     7\n",
       "10594    8\n",
       "37445    1\n",
       "12928    1\n",
       "2747     5\n",
       "6658     9\n",
       "8966     6\n",
       "Name: label, dtype: int64"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([3, 6, 9, 5, 6, 0, 6, 0, 0, 1, 7, 1, 5, 7, 8, 1, 1, 5, 9, 6],\n",
       "      dtype=int64)"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CPU times: total: 1h 25min 34s\n",
      "Wall time: 8min 10s\n"
     ]
    }
   ],
   "source": [
    "%%time\n",
    "from sklearn.linear_model import LogisticRegression # 刚才的错，sklearn版本的问题，升级了一下，截距\n",
    "model =LogisticRegression(max_iter=5000)\n",
    "model.fit(X_train,y_train)\n",
    "y_ = model.predict(X_test)\n",
    "display(y_test[:20],y_[:20])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "ddbfccfb",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-06-17T13:00:14.390949Z",
     "start_time": "2022-06-17T13:00:14.365029Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CPU times: total: 15.6 ms\n",
      "Wall time: 9.98 ms\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "0.901"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%%time\n",
    "model.score(X_test,y_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "36b6d024",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-06-17T13:15:38.156768Z",
     "start_time": "2022-06-17T13:12:37.593375Z"
    },
    "collapsed": true
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "d:\\soft\\python\\396\\lib\\site-packages\\sklearn\\linear_model\\_logistic.py:444: ConvergenceWarning: lbfgs failed to converge (status=1):\n",
      "STOP: TOTAL NO. of ITERATIONS REACHED LIMIT.\n",
      "\n",
      "Increase the number of iterations (max_iter) or scale the data as shown in:\n",
      "    https://scikit-learn.org/stable/modules/preprocessing.html\n",
      "Please also refer to the documentation for alternative solver options:\n",
      "    https://scikit-learn.org/stable/modules/linear_model.html#logistic-regression\n",
      "  n_iter_i = _check_optimize_result(\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "16275    3\n",
       "19204    6\n",
       "18518    9\n",
       "25780    5\n",
       "16228    6\n",
       "15824    5\n",
       "29252    6\n",
       "28482    0\n",
       "13779    0\n",
       "25912    1\n",
       "27141    7\n",
       "21848    1\n",
       "32576    5\n",
       "7975     7\n",
       "10594    8\n",
       "37445    1\n",
       "12928    1\n",
       "2747     5\n",
       "6658     9\n",
       "8966     6\n",
       "Name: label, dtype: int64"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([3, 6, 9, 5, 6, 0, 6, 0, 0, 1, 7, 1, 5, 7, 8, 1, 1, 5, 9, 6],\n",
       "      dtype=int64)"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CPU times: total: 20min 13s\n",
      "Wall time: 3min\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "0.928"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%%time\n",
    "# PCA降维没有进行归一化\n",
    "from sklearn.linear_model import LogisticRegression # 刚才的错，sklearn版本的问题，升级了一下，截距\n",
    "model =LogisticRegression(max_iter=5000)\n",
    "model.fit(X_train_pca,y_train)\n",
    "y_ = model.predict(X_test_pca)\n",
    "display(y_test[:20],y_[:20])\n",
    "model.score(X_test_pca,y_test)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "26396f9e",
   "metadata": {},
   "source": [
    "#### 降维时候，归一化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "1fef424e",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-06-17T13:17:37.230005Z",
     "start_time": "2022-06-17T13:17:29.982524Z"
    }
   },
   "outputs": [],
   "source": [
    "pca = PCA(n_components=0.95,whiten=True) \n",
    "# 标准化处理，算法KNN，距离计算，不需要标准化，如果是梯度下降算法【必须归一化】\n",
    "X_pca = pca.fit_transform(X)\n",
    "X_train_pca,X_test_pca,y_train,y_test = train_test_split(X_pca,y,test_size = 1000,random_state = 0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "dd626141",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-06-17T13:17:58.263752Z",
     "start_time": "2022-06-17T13:17:52.859501Z"
    },
    "collapsed": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "16275    3\n",
       "19204    6\n",
       "18518    9\n",
       "25780    5\n",
       "16228    6\n",
       "15824    5\n",
       "29252    6\n",
       "28482    0\n",
       "13779    0\n",
       "25912    1\n",
       "27141    7\n",
       "21848    1\n",
       "32576    5\n",
       "7975     7\n",
       "10594    8\n",
       "37445    1\n",
       "12928    1\n",
       "2747     5\n",
       "6658     9\n",
       "8966     6\n",
       "Name: label, dtype: int64"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "array([3, 6, 9, 5, 6, 0, 6, 0, 0, 1, 7, 1, 5, 7, 8, 1, 1, 5, 9, 6],\n",
       "      dtype=int64)"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CPU times: total: 35.8 s\n",
      "Wall time: 5.38 s\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "0.929"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%%time\n",
    "# PCA降维进行归一化\n",
    "from sklearn.linear_model import LogisticRegression # 刚才的错，sklearn版本的问题，升级了一下，截距\n",
    "model =LogisticRegression(max_iter=5000)\n",
    "model.fit(X_train_pca,y_train)\n",
    "y_ = model.predict(X_test_pca)\n",
    "display(y_test[:20],y_[:20])\n",
    "model.score(X_test_pca,y_test)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0550cd9b",
   "metadata": {},
   "source": [
    "### LDA线性判别"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "ccf91bd1",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-06-17T13:30:59.414589Z",
     "start_time": "2022-06-17T13:30:59.331338Z"
    }
   },
   "outputs": [],
   "source": [
    "from sklearn import datasets"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "88d0472c",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-06-17T13:31:29.472818Z",
     "start_time": "2022-06-17T13:31:29.464408Z"
    }
   },
   "outputs": [],
   "source": [
    "from sklearn.discriminant_analysis import LinearDiscriminantAnalysis"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "c5d0f375",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-06-17T13:31:48.506657Z",
     "start_time": "2022-06-17T13:31:48.485714Z"
    }
   },
   "outputs": [],
   "source": [
    "X,y = datasets.load_iris(return_X_y=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "0d909467",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-06-17T13:33:06.917965Z",
     "start_time": "2022-06-17T13:33:06.912577Z"
    }
   },
   "outputs": [],
   "source": [
    "# solver : {'svd', 'lsqr', 'eigen'}\n",
    "lda = LinearDiscriminantAnalysis(solver='eigen',n_components=2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "bc9c585f",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-06-17T13:55:09.248833Z",
     "start_time": "2022-06-17T13:55:09.231875Z"
    }
   },
   "outputs": [],
   "source": [
    "X_lda = lda.fit_transform(X,y) # 有监督，数据X对应着y\n",
    "# pca.fit_transform(X) # 无监督，只需要传入X，降维"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "164a3c98",
   "metadata": {},
   "source": [
    "### 自己写代码完成LDA操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "7a7454fe",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-06-17T13:51:50.137248Z",
     "start_time": "2022-06-17T13:51:50.120238Z"
    }
   },
   "outputs": [],
   "source": [
    "np.set_printoptions(suppress=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "7241366d",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-06-17T13:57:14.888168Z",
     "start_time": "2022-06-17T13:57:14.857724Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[6.01716893, 7.03257409],\n",
       "       [5.0745834 , 5.9344564 ],\n",
       "       [5.43939015, 6.46102462],\n",
       "       [4.75589325, 6.05166375],\n",
       "       [6.08839432, 7.24878907]])"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# scipy比numpy更加高级的科学计算库\n",
    "# pip install scipy\n",
    "from scipy import linalg # 线性代数\n",
    "# 1、总的散度矩阵\n",
    "# 协方差，计算的列的,Scatter _ total\n",
    "St = np.cov(X.T,rowvar = True,bias = 1) # bias偏差，截距\n",
    "# 2、类内散度矩阵，分3类：0、1、2\n",
    "# Sw within类内\n",
    "Sw = np.full(shape = (4,4),fill_value=0,dtype=np.float64) # 声明了一个空的，全是0\n",
    "for i in range(3): # i = 0,1,2\n",
    "    Sw += np.cov(X[y ==i],rowvar=False,bias=1)\n",
    "Sw/=3 # 三个类别的平均【类内的散度矩阵】\n",
    "# 3、计算类间的散度矩阵\n",
    "Sb = St - Sw\n",
    "# 4、计算特征值和特征向量\n",
    "eigen,ev = linalg.eigh(Sb,Sw) # 类间和类内，特征值从小到大\n",
    "np.argsort(eigen) # 排序索引\n",
    "n_components = 2\n",
    "ev = ev[:,[3,2,1,0]][:,:n_components] # 从大到小\n",
    "# 5、进行矩阵运算\n",
    "X.dot(ev)[:5]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "0026a9b4",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-06-17T13:55:19.381753Z",
     "start_time": "2022-06-17T13:55:19.357537Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[6.01716893, 7.03257409],\n",
       "       [5.0745834 , 5.9344564 ],\n",
       "       [5.43939015, 6.46102462],\n",
       "       [4.75589325, 6.05166375],\n",
       "       [6.08839432, 7.24878907]])"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X_lda[:5]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "59593e9f",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.6"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
